package com.springdoor.goods.service.impl;

import java.util.List;

import com.springdoor.goods.domain.SprdCommonImages;
import com.springdoor.goods.mapper.SprdCommonImagesMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springdoor.goods.mapper.SprdGoodsImgMapper;
import com.springdoor.goods.domain.SprdGoodsImg;
import com.springdoor.goods.service.ISprdGoodsImgService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品图片Service业务层处理
 * 
 * @author llshs
 * @date 2024-05-31
 */
@Service
public class SprdGoodsImgServiceImpl implements ISprdGoodsImgService 
{
    @Autowired
    private SprdGoodsImgMapper imgMapper;

    @Autowired
    private SprdCommonImagesMapper commonImagesMapper;

    /**
     * 查询商品图片
     * 
     * @param imgId 商品图片主键
     * @return 商品图片
     */
    @Override
    public SprdGoodsImg selectSprdGoodsImgByImgId(Long imgId)
    {
        return imgMapper.selectSprdGoodsImgByImgId(imgId);
    }

    /**
     * 查询商品图片列表
     * 
     * @param sprdGoodsImg 商品图片
     * @return 商品图片
     */
    @Override
    public List<SprdGoodsImg> selectSprdGoodsImgList(SprdGoodsImg sprdGoodsImg)
    {
        return imgMapper.selectSprdGoodsImgList(sprdGoodsImg);
    }

    @Override
    public List<SprdGoodsImg> selectSprdGoodsImgListByGNumber(String gNumber) {
        return imgMapper.selectSprdGoodsImgListByGNumber(gNumber);
    }

    /**
     * 新增商品图片
     * 
     * @param sprdGoodsImg 商品图片
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSprdGoodsImg(SprdGoodsImg sprdGoodsImg)
    {
        int row = 0;
        Long orderNum = sprdGoodsImg.getOrderNum();
        if (orderNum == null || orderNum <= 0) {
            orderNum = imgMapper.findMaxOrderNum(sprdGoodsImg.getgNumber());
        }
        if (StringUtils.isNotEmpty(sprdGoodsImg.getImgPath())) {
            String[] split = sprdGoodsImg.getImgPath().split(",");
            boolean uploadFlg = StringUtils.equals("1", (String) sprdGoodsImg.getParams().get("uploadFlag"));
            int maxOrder = 1;
            if (uploadFlg) {
                maxOrder = commonImagesMapper.findMaxOrderNum(sprdGoodsImg.getgNumber());
            }
            for (String path : split) {
                if (uploadFlg) {
                    insertCommonImages(sprdGoodsImg, maxOrder++);
                }
                sprdGoodsImg.setOrderNum(orderNum++);
                sprdGoodsImg.setImgPath(path);
                row += imgMapper.insertSprdGoodsImg(sprdGoodsImg);
            }
        }
        return row;
    }

    private int insertCommonImages(SprdGoodsImg img, int orderNum) {
        SprdCommonImages commonImages = new SprdCommonImages();
        commonImages.setCreateBy(img.getUpdateBy());
        commonImages.setUpdateBy(img.getUpdateBy());
        commonImages.setgNumber(img.getgNumber());
//        commonImages.setgName(img.getGoodsName());
        commonImages.setImageName(img.getImgName());
        commonImages.setImageType(img.getImgType());
        commonImages.setImageServer("qiniu");
        commonImages.setImagePath(img.getImgPath());
        commonImages.setOrderNum(orderNum);
        return commonImagesMapper.insertSprdCommonImages(commonImages);
    }

    /**
     * 修改商品图片
     * 
     * @param sprdGoodsImg 商品图片
     * @return 结果
     */
    @Override
    public int updateSprdGoodsImg(SprdGoodsImg sprdGoodsImg)
    {
        return imgMapper.updateSprdGoodsImg(sprdGoodsImg);
    }

    /**
     * 批量删除商品图片
     * 
     * @param imgIds 需要删除的商品图片主键
     * @return 结果
     */
    @Override
    public int deleteSprdGoodsImgByImgIds(Long[] imgIds)
    {
        return imgMapper.deleteSprdGoodsImgByImgIds(imgIds);
    }

    /**
     * 删除商品图片信息
     * 
     * @param imgId 商品图片主键
     * @return 结果
     */
    @Override
    public int deleteSprdGoodsImgByImgId(Long imgId)
    {
        return imgMapper.deleteSprdGoodsImgByImgId(imgId);
    }

    @Override
    public Long findMaxOrderNum(String gNumber) {
        return imgMapper.findMaxOrderNum(gNumber);
    }
}
